﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SOL.GLReading.Layers.Model;

namespace Wicresoft.SOL.GLReading.Controllers
{ 
    public class RecommendController : Controller
    {
        private GLReadingEntities db = new GLReadingEntities();

        //
        // GET: /Recommend/

        public ViewResult Index()
        {

            var recommend = db.Recommend.Include("Book");
            return View(recommend.ToList());
        }

        //
        // GET: /Recommend/Details/5

        public ViewResult Details(Guid id)
        {
            Recommend recommend = db.Recommend.Single(r => r.ID == id);
            return View(recommend);
        }

        //
        // GET: /Recommend/Create

        public ActionResult Create()
        {
            ViewBag.BookID = new SelectList(db.Book, "ID", "Type");
            return View();
        } 

        //
        // POST: /Recommend/Create

        [HttpPost]
        public ActionResult Create(Recommend recommend)
        {
            if (ModelState.IsValid)
            {
                recommend.ID = Guid.NewGuid();
                db.Recommend.AddObject(recommend);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.BookID = new SelectList(db.Book, "ID", "Type", recommend.BookID);
            return View(recommend);
        }
        
        //
        // GET: /Recommend/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            Recommend recommend = db.Recommend.Single(r => r.ID == id);
            ViewBag.BookID = new SelectList(db.Book, "ID", "Type", recommend.BookID);
            return View(recommend);
        }

        //
        // POST: /Recommend/Edit/5

        [HttpPost]
        public ActionResult Edit(Recommend recommend)
        {
            if (ModelState.IsValid)
            {
                db.Recommend.Attach(recommend);
                db.ObjectStateManager.ChangeObjectState(recommend, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BookID = new SelectList(db.Book, "ID", "Type", recommend.BookID);
            return View(recommend);
        }

        //
        // GET: /Recommend/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            Recommend recommend = db.Recommend.Single(r => r.ID == id);
            return View(recommend);
        }

        //
        // POST: /Recommend/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {            
            Recommend recommend = db.Recommend.Single(r => r.ID == id);
            db.Recommend.DeleteObject(recommend);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}